package com.hospital.workstation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hospital.common.annotation.Log;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.workstation.domain.bo.MedicationSummaryBo;
import com.hospital.workstation.domain.vo.MedicationSummaryVo;
import com.hospital.workstation.service.IMedicationSummaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * 药历小结信息
 *
 * @author hospital
 * @date 2023-02-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/medicationSummary")
public class MedicationSummaryController extends BaseController {

    private final IMedicationSummaryService iMedicationSummaryService;

    /**
     * 新增药历小结信息
     */
    @SaCheckPermission("workstation:medicationSummary:add")
    @Log(title = "药历小结信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MedicationSummaryBo bo) {
        try {
            return iMedicationSummaryService.insertByBo(bo);
        } catch (Exception e) {
            return R.fail("保存失败");
        }
    }

    /**
     * 根据病历号查询药历小结信息
     *
     * @param medicalRecordNo 病历号
     * @return
     */
    @SaCheckPermission("workstation:medicationSummary:query")
    @GetMapping("/{medicalRecordNo}")
    public R<MedicationSummaryVo> getInfo(@NotNull(message = "药历号不能为空")
                                          @PathVariable Integer medicalRecordNo) {
        return R.ok(iMedicationSummaryService.queryByMedicalRecordNo(medicalRecordNo));
    }

    /**
     * 下载药学监护小结
     *
     * @param response
     * @param medicalRecordNo 药历号
     */
    @SaCheckPermission("workstation:medicationSummary:export")
    @GetMapping("/export/{medicalRecordNo}")
    public void exportPharmaceuticalCareSummary(HttpServletResponse response, @PathVariable Integer medicalRecordNo) {
        iMedicationSummaryService.exportPharmaceuticalCareSummary(response, medicalRecordNo);
    }

    /**
     * 完成并存档药历
     *
     * @param medicalRecordNo 流水号
     * @return
     */
    @GetMapping("/state/{medicalRecordNo}")
    public R<Void> setSubmitState(@PathVariable Integer medicalRecordNo) {
        return iMedicationSummaryService.setSubmitState(medicalRecordNo);
    }
}
